package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.FenceAdapter;
import com.jt.tenant.conn.SetDelFence;
import com.jt.tenant.conn.GetQueryFence;
import com.jt.tenant.dialog.TipDialog;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AD on 2016/8/17.
 */
public class FenceListActivity extends JTActivity implements View.OnClickListener{

    private TextView textViewTitle;

    private TextView add_fence;

    private ListView fenceList;

    private TipDialog tipDialog;

    private static OnChangeCallBack OnChangeCallBack;

    private List<GetQueryFence.Fence> list = new ArrayList<>();

    private FenceAdapter adapter;

    public static DeleteFence deleteFence;

    private int deletePosition = 0;

    private GetQueryFence getQueryFence = new GetQueryFence(new AsyCallBack<List<GetQueryFence.Fence>>(){

        @Override
        public void onSuccess(String toast, int type, List<GetQueryFence.Fence> info) throws Exception {

            list.clear();

            list.addAll(info);

            adapter.notifyDataSetChanged();


        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            if(!toast.equals("000")){

                UtilToast.show(FenceListActivity.this, toast);

            }
        }

    });



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fence_list);

        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();

        getQueryFence.execute(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        deleteFence = null;

    }

    private void initView(){

        textViewTitle = (TextView) findViewById(R.id.tv_title_name);

        textViewTitle.setText("电子围栏列表");

        (add_fence = (TextView) findViewById(R.id.tv_fence_add)).setOnClickListener(this);

        fenceList = (ListView) findViewById(R.id.fence_list);

        adapter = new FenceAdapter(context,list);

        fenceList.setAdapter(adapter);

        fenceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(FenceListActivity.this, FenceActivity.class).putExtra("Fence", list.get(position)));
            }
        });

        fenceList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                deletePosition = position;

                tipDialog = new TipDialog(context);

                tipDialog.setTip("您确定要删除此条电子围栏么？");

                tipDialog.setType(1);

                tipDialog.show();

                return true;
            }
        });

        OnChangeCallBack = new OnChangeCallBack() {

            @Override
            public void onChange(){

                getQueryFence.is_open = "1";

                getQueryFence.execute(FenceListActivity.this);

            }

        };

        deleteFence = new DeleteFence() {
            @Override
            public void delete() {
                new SetDelFence(list.get(deletePosition).id, new AsyCallBack() {

                    @Override
                    public void onSuccess(String toast, int type, Object o) throws Exception {

                        list.remove(list.get(deletePosition));

                        adapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                        Log.e("aaaaa", "失败");
                    }
                }).execute(FenceListActivity.this);
            }
        };


    }

    @Override
    public void onDetachedFromWindow() {

        super.onDetachedFromWindow();

        OnChangeCallBack = null;

    }

    public static void Refresh(){

        if(OnChangeCallBack != null){

            OnChangeCallBack.onChange();

        }

    }

    public interface OnChangeCallBack{

        void onChange();

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.tv_fence_add:

                startActivity(new Intent(context, FenceActivity.class));

                break;

        }

    }

    public abstract class DeleteFence{

        public abstract void delete();

    }
}
